
$(function(){
	initView();
})
var contractId;
var policyRef;
var insured;
var pdfType="1166";
function initView(){

//	contractId=sessionStorage.contractId;
//	policyRef=sessionStorage.policyRef;
	contractId = getQueryString("contractId");
	policyRef = getQueryString("policyRef");
//	insured = getQueryString("insured");
//	contractId="256041";
//	policyRef="0000023142";
	
	console.log(contractId);
	console.log(policyRef);
	 /*	$.get('/pad_marketing/main/getSign.action',{'jsonParam':JSON.stringify({signKey:'136NEWTOUCH'})},function(data){
		console.log(data);
	});*/
	
	/***	事件管理	***/
	//投保人“点击签字”
	$('#sign1').click(function(){
		goSign1('showSign','pdf6','0','img1');
	});
	$('#img1').click(function(){
		goSign1('showSign','pdf6','0','img1');
	});
	//营销员“点击签字”
	$('#sign2').click(function(){
		goSign1('showSign','pdf6','1','img2');
	});
	$('#img2').click(function(){
		goSign1('showSign','pdf6','1','img2');
	});
	//投保人点击拍照
	$('#pho1').click(function(){
		takeSignPhoto('showPhoto','pdf6','0','pho1');
	});
	//营销员点击拍照
	$('#pho2').click(function(){
		takeSignPhoto('showPhoto','pdf6','1','pho2');
	});
	
	//goSign1('showSign','pdf1','0','img1')		pdf1第几张pdf；0坐标信息；img1图片
	
	showLoadingPad();
	padMarketing.ajaxNormal(
			"/pad_marketing/note/downloadEReceipt.action",
				{"contractId":contractId,"policyRef":policyRef,"pdfType":pdfType},
				function(data){
					console.log(data.recptStatus);
					if(data.recptStatus == "unSign"){
						var len = data.len;
						len=parseInt(len);
						for(var i=0;i<len;i++){
							var html="<img id='content"+i+"' alt='' src=''>";
							$("#pdfContent").append(html);
							$('#content'+i).attr(
									'src',
									'/pad_marketing/note/showJpgs.action?id='+i+'&contractId='+contractId+'&policyRef='+policyRef +'&pdfType=' + pdfType
									).load(function(){
								$(this).css("width",$(window).width()-50);
								if(myScroll){
									myScroll.refresh();
								}
							});
						}
						
						padMarketing.ajaxNormal(
								"/pad_marketing/note/getInsured.action",
									{"contractId":contractId},
									function(data){
										insured = data.PRE_PRINTED_NO;
										window.setTimeout(function(){
											  initSign(policyRef,'5');
										    },500)
										hideLoadingPad();
										console.log(insured);
									}
						);
					}else{
						alertMsg('亲，已签过字了',0,function(){
							goBack();
						});
					}
				}
	);
	
	
	//确认按钮
	$('#affirm').click(function(){
		showLoadingPad();
		signComplete();
//		changeState();
		
	})
	

}


   

/**
 * 壳回调函数，展示签字图片
 * @param src
 * @param pdfNum
 * @param siger
 * @param imgNum
 */
function showSign(src,pdfNum,siger,imgNum){
	var num = imgNum.substr(3,1);
	$("#sign"+num).hide();
//	$("#"+imgNum).hide();
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}

/**
 * 展示拍照头像
 * 
 * @param src
 * @param imgNum
 */
var src1 = "";
var src2 = "";
function showPhoto(src,imgNum){
	if('pho1'==imgNum)src1 = src;
	if('pho2'==imgNum)src2 = src;
	var img=document.getElementById(imgNum);
	src = 'data:image/png;base64,' + src;			
	img.src = src;
	if(myScroll)
		myScroll.refresh();
}

function signComplete(){
	//投保单--投保人签名
	var sign1 = $("#sign1").attr("style");
	if('display: none;'!=sign1 ){
		hideLoadingPad();
		alertMsg('请完成投保单（投保人）签名！');
		return;
	}
	//校验投保单--投保人拍照
	if(''==src1){
		hideLoadingPad();
		alertMsg('请完成投保单（投保人）清晰的胸部以上正面照拍摄！');
		return;
	}
	
	//投保单--投保人签名
	var sign2 = $("#sign2").attr("style");
	if('display: none;'!=sign2 ){
		hideLoadingPad();
		alertMsg('请完成投保单（营销人员）签名！');
		return;
	}
	//校验投保单--投保人拍照
	if(''==src2){
		hideLoadingPad();
		alertMsg('请完成投保单（营销人员）清晰的胸部以上正面照拍摄！');
		return;
	}
	
	//调壳生成加密字符串
	finishSign("doSign");
	
	
}

/**
 *  半身照生成加密文件的回调函数
 * @param pdf1
 * @param pdf2
 */
function doSign(pdf6){
	var photoList1 = [src1,src2];
	var pdfMessageList = [];
	pdfMessageList.push({"pdfType":pdfType,"encode":pdf6,"photoList":photoList1});
	var params={"policyRef":policyRef,"pdfMessageList":pdfMessageList,"insured":insured,"sign":contractId};
	padMarketing.ajaxNormal('eReceipt_uploadCA.action',params,
			function(data){
		       
		       changeState();
				
			},function(data){
				
			},
			true,//是否异步
			true,//是否显示加载电波图
			'不用美图都那么帅，不过字该练练啦！'//电波图中的话术
	);
}

function changeState(){
//	contractId = "256041";
	var param={"contractId":contractId};
	padMarketing.ajaxNormal(
			"eReceiptUpdateState.action",
			param,
			function(data){
				hideLoadingPad();
				console.log(data);
				if(data.result == 0){
					alertMsg('签字完成',0,function(){
						goBack();
					});
				}else{
					alertMsg('更新状态错误');
				}
			}
	);
}



/**
 * 回退
 */
function goBack(){
	var url = getQueryString("url");
	if(url==null){
		location.href="/pad_marketing/index/index.action";
		return;
	}
	location.href=url;
}
